﻿Imports System.Data.SqlClient
Public Class Client
    Public Id As Integer
    Public Nom As String
    Public Prenom As String
    Public Cin As String
    Public Nom_ent As String
    Public Representant_legal As String
    Public Occupation As String
    Public Adress As String
    Public Email As String
    Public Ville As String
    Public Type As String
    Public Remarque As String
    Public photo() As Byte

    'Les constructeur ------------------------

    Sub New(ByVal Id As Integer, ByVal Nom As String, ByVal Prenom As String, ByVal Cin As String, ByVal Nom_ent As String, ByVal Representant_legal As String, ByVal Occupation As String, ByVal Adress As String, ByVal Email As String, ByVal Ville As String, ByVal Type As String, ByVal Remarque As String, ByVal photo() As Byte)
        Me.Id = Id
        Me.Nom = Nom
        Me.Prenom = Prenom
        Me.Cin = Cin
        Me.Nom_ent = Nom_ent
        Me.Representant_legal = Representant_legal
        Me.Occupation = Occupation
        Me.Adress = Adress
        Me.Email = Email
        Me.Ville = Ville
        Me.Type = Type
        Me.Remarque = Remarque
        Me.photo = photo
    End Sub

    Sub New(ByVal enregistrement As ArrayList)
        Me.Id = enregistrement.Item(0)
        Me.Nom = enregistrement.Item(1)
        Me.Prenom = enregistrement.Item(2)
        Me.Cin = enregistrement.Item(3)
        Me.Adress = enregistrement.Item(4)
        Me.Email = enregistrement.Item(5)
        Me.Nom_ent = enregistrement.Item(6)
        Me.Representant_legal = enregistrement.Item(7)
        Me.Occupation = enregistrement.Item(8)
        Me.Ville = enregistrement.Item(9)
        Me.Type = enregistrement.Item(10)
        Me.Remarque = enregistrement.Item(11)
        Me.photo = enregistrement.Item(12)
    End Sub

    Sub New()

    End Sub

    'les listes ------------------------------

    Public list_phone As New ArrayList
    Public List_montant As New ArrayList

    'les fonctions ------------------------

    Public Sub remplir_List_montant()
        Me.List_montant.Clear()
        Dim cmd As New SqlClient.SqlCommand("select * from Montants where Id_Client =" & Me.Id & "      ", Partager.con)
        Partager.Open()
        Partager.dr = cmd.ExecuteReader
        While Partager.dr.Read
            Dim khab As New Montant(Partager.dr("id"), Partager.dr("Montant"), Partager.dr("Date"), Partager.dr("Remarque"), Partager.dr("Type"))
            Me.List_montant.Add(khab)
        End While
        Partager.Close()
    End Sub

    Public Sub suprimmer_montant(ByVal id As Integer, ByVal type As String)

        Dim cmd As New SqlClient.SqlCommand("delete from Montants where id =" & id & "and type ='" & type & "'")

        Partager.Open()
        cmd.ExecuteNonQuery()
        Partager.Close()

        Me.remplir_List_montant()


    End Sub

    Public Sub Ajouter_montant(ByVal mnt As Montant)

        Dim cmd As New SqlClient.SqlCommand("insert into Montants values (@Id_Client,@Montant,@Date,@Type,@Remarque)", Partager.con)

        cmd.Parameters.Add("@Id_Client", SqlDbType.VarChar).Value = Me.Id
        cmd.Parameters.Add("@Montant", SqlDbType.VarChar).Value = mnt.Montant
        cmd.Parameters.Add("@Date", SqlDbType.VarChar).Value = mnt.Date_payement
        cmd.Parameters.Add("@Type", SqlDbType.VarChar).Value = mnt.Type

        cmd.Parameters.Add("@Remarque", SqlDbType.VarChar).Value = mnt.Remarque


        Partager.Open()
        cmd.ExecuteNonQuery()
        Partager.Close()

        Me.remplir_List_montant()


    End Sub

    Public Sub modifier_montant(ByVal mnt As Montant, ByVal idd As Integer)

        Dim cmd As New SqlClient.SqlCommand("update  Montants set Id_Client=@Id_Client,Montant=@Montant , Date=@Date,  Type=@Type ,Remarque=Remarque where id=" & idd & ")", Partager.con)

        cmd.Parameters.Add("@Id_Client", SqlDbType.VarChar).Value = Me.Id
        cmd.Parameters.Add("@Montant", SqlDbType.VarChar).Value = mnt.Montant
        cmd.Parameters.Add("@Date", SqlDbType.VarChar).Value = mnt.Date_payement
        cmd.Parameters.Add("@Type", SqlDbType.VarChar).Value = mnt.Type

        cmd.Parameters.Add("@Remarque", SqlDbType.VarChar).Value = mnt.Remarque



        Partager.Open()
        cmd.ExecuteNonQuery()
        Partager.Close()

        Me.remplir_List_montant()

    End Sub




    Public Sub remplir_List_list_phone()
        Me.list_phone.Clear()
        Partager.ad = New SqlDataAdapter("select * from phone where id_Client =" & Me.Id, Partager.conPhone)
        Partager.ds = New DataSet
        Partager.ad.Fill(Partager.ds, "phone")
        For Each f As DataRow In Partager.ds.Tables(0).Rows
            If f("Num") Is DBNull.Value Then
                f("Num") = ""
            End If
            Dim khab As New Phone(f("id"), f("Num"), f("Type"))
            Me.list_phone.Add(khab)
        Next
        'Partager.Close()
    End Sub

    Public Sub suprimmer_phone(ByVal id As Integer, ByVal type As String)

        Dim cmd As New SqlClient.SqlCommand("delete from phone where Type='" & type & "' And id_Client=" & id, Partager.con)

        Partager.Open()
        cmd.ExecuteNonQuery()
        Partager.Close()

        'Me.remplir_List_list_phone()


    End Sub

    Public Sub Ajouter_phone(ByVal ph As Phone)

        Dim cmd As New SqlClient.SqlCommand("insert into phone(Num,Type,Id_Client) values (@Num,@Type,@Id_Clients)", Partager.con)

        cmd.Parameters.Add("@Id_Clients", SqlDbType.VarChar).Value = Me.Id
        cmd.Parameters.Add("@Type", SqlDbType.VarChar).Value = ph.Type
        cmd.Parameters.Add("@Num", SqlDbType.VarChar).Value = ph.Num


        Partager.Open()
        cmd.ExecuteNonQuery()
        Partager.Close()

        'Me.remplir_List_list_phone()
    End Sub

    Public Sub modifier_List_phone(ByVal idd As Integer, ByVal ph As Phone)
        Dim cmd As New SqlClient.SqlCommand("update phone set  Num=@Num where id" & Id, Partager.con)
        cmd.Parameters.Add("@Num", SqlDbType.VarChar).Value = ph.Num
        Partager.Open()
        cmd.ExecuteNonQuery()
        Partager.Close()

        Me.remplir_List_list_phone()
    End Sub

End Class
